using System.Collections;

namespace NavigateExample.Services.Navigation
{
    /// <summary>
    /// 导航参数类，支持键值对形式的参数传递
    /// </summary>
    //实现了IEnumerable<KeyValuePair<string, object>>接口。
    //就可以像遍历一个字典一样，直接使用foreach循环来遍历DialogParameters对象实例中的所有键值对。
    public class NavigationParameters : IEnumerable<KeyValuePair<string, object>>
    {
        private readonly Dictionary<string, object> _parameters;

        public NavigationParameters()
        {
            _parameters = new Dictionary<string, object>();
        }

        public NavigationParameters(Dictionary<string, object> parameters)
        {
            _parameters = parameters ?? new Dictionary<string, object>();
        }

        /// <summary>
        /// 添加参数
        /// </summary>
        /// <param name="key">参数键</param>
        /// <param name="value">参数值</param>
        public void Add(string key, object value)
        {
            _parameters[key] = value;
        }

        /// <summary>
        /// 获取参数值
        /// </summary>
        /// <typeparam name="T">参数类型</typeparam>
        /// <param name="key">参数键</param>
        /// <returns>参数值</returns>
        public T? GetValue<T>(string key)
        {
            if (_parameters.TryGetValue(key, out var value) && value is T typedValue)
            {
                return typedValue;
            }
            return default(T);
        }

        /// <summary>
        /// 获取参数值，如果不存在则返回默认值
        /// </summary>
        /// <typeparam name="T">参数类型</typeparam>
        /// <param name="key">参数键</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns>参数值或默认值</returns>
        public T GetValue<T>(string key, T defaultValue)
        {
            if (_parameters.TryGetValue(key, out var value) && value is T typedValue)
            {
                return typedValue;
            }
            return defaultValue;
        }

        /// <summary>
        /// 检查是否包含指定键
        /// </summary>
        /// <param name="key">参数键</param>
        /// <returns>是否包含</returns>
        public bool ContainsKey(string key)
        {
            return _parameters.ContainsKey(key);
        }

        /// <summary>
        /// 参数数量
        /// </summary>
        public int Count => _parameters.Count;

        /// <summary>
        /// 索引器，支持通过键获取和设置值
        /// </summary>
        /// <param name="key">参数键</param>
        /// <returns>参数值</returns>
        public object? this[string key]
        {
            get => _parameters.TryGetValue(key, out var value) ? value : null;
            set => _parameters[key] = value!;
        }

        /// <summary>
        /// 获取所有键
        /// </summary>
        public IEnumerable<string> Keys => _parameters.Keys;

        /// <summary>
        /// 获取所有值
        /// </summary>
        public IEnumerable<object> Values => _parameters.Values;

        public IEnumerator<KeyValuePair<string, object>> GetEnumerator()
        {
            return _parameters.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    }
}